<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="业主姓名" prop="ownerName">
        <el-input
          v-model="queryParams.ownerName"
          placeholder="请输入业主姓名"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业主手机号" prop="ownerPhone">
        <el-input
          v-model="queryParams.ownerPhone"
          placeholder="请输入业主手机号"
          clearable
          style="width: 200px"
          maxlength="11"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同编号" prop="contractNo">
        <el-input
          v-model="queryParams.contractNo"
          placeholder="请输入合同编号"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房屋性质" prop="propertyType">
        <el-select
          v-model="queryParams.propertyType"
          clearable
          style="width: 200px"
        >
          <el-option label="出租" :value="1"> </el-option>
          <el-option label="出售" :value="2"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="房屋状态" prop="propertyStatus">
        <el-select
          v-model="queryParams.propertyStatus"
          clearable
          style="width: 200px"
        >
          <el-option label="未租/未售" :value="1"> </el-option>
          <el-option label="已租/已售" :value="2"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:contract:transactionType:add']"
          >新增</el-button
        >
      </el-col> -->
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="dataList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="业主" align="center" prop="ownerName" />
      <el-table-column label="业主手机号" align="center" prop="ownerPhone" />
      <el-table-column label="归属公司" align="center" prop="companyName" />
      <el-table-column label="归属门店" align="center">
        <template #default="scope">
          {{ scope.row.regionName + "" + scope.row.storeName }}
        </template>
      </el-table-column>
      <el-table-column label="房源地址" align="center" prop="detailAddress" />
      <el-table-column
        label="房屋性质"
        align="center"
        prop="propertyTypeName"
      />
      <el-table-column
        label="房屋状态"
        align="center"
        prop="propertyStatusName"
      />
      <el-table-column label="合同编号" align="center" prop="contractNo" />

      <el-table-column
        label="操作"
        width="180"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Document"
            @click="handleDetail(scope.row)"
            v-hasPermi="['system:contract:transactionType:edit']"
            >查看</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:contract:transactionType:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-descriptions
        class="property-detail"
        title="房源详情"
        :column="2"
        border
      >
        <!-- 归属信息 -->
        <el-descriptions-item label="归属公司">{{
          activeRow.companyName
        }}</el-descriptions-item>
        <el-descriptions-item label="归属门店">{{
          activeRow.regionName + activeRow.storeName
        }}</el-descriptions-item>

        <el-descriptions-item label="合同编号">{{
          activeRow.contractNo
        }}</el-descriptions-item>

        <!-- 地址信息 -->
        <el-descriptions-item label="房源地址" :span="2">
          {{ activeRow.detailAddress }}
        </el-descriptions-item>

        <!-- 业主信息 -->
        <el-descriptions-item label="业主">{{
          activeRow.ownerName
        }}</el-descriptions-item>
        <el-descriptions-item label="业主电话">{{
          activeRow.ownerPhone
        }}</el-descriptions-item>

        <!-- 房屋状态 -->
        <el-descriptions-item label="房屋性质">{{
          activeRow.propertyTypeName
        }}</el-descriptions-item>
        <el-descriptions-item label="房屋状态">
          <el-tag
            :type="activeRow.propertyStatusName === '在售' ? 'success' : 'info'"
          >
            {{ activeRow.propertyStatusName }}
          </el-tag>
        </el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="cancel">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="house">
import { listHouse, delHouse } from "@/api/business/house";
const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");

const dataList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    businessName: undefined,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);
const activeRow = ref({});

/** 查询岗位列表 */
function getList() {
  loading.value = true;
  listHouse(queryParams.value).then((response) => {
    dataList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.postId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 查看按钮操作 */
function handleDetail(row) {
  activeRow.value = row;
  open.value = true;
}

/** 删除按钮操作 */
function handleDelete(row) {
  const id = row.id || ids.value;
  proxy.$modal
    .confirm("是否确认删除？")
    .then(function () {
      return delHouse(id);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "system/post/export",
    {
      ...queryParams.value,
    },
    `post_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
